package com.base.mine;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseMvpActivity;
import com.base.mine.config.RouteConfig;
import com.base.mine.databinding.ActivityLoginBinding;
import com.base.mine.presenter.LoginPresenter;
import com.base.mine.utils.Rotate3D;
import com.base.mine.view.LoginView;

/**
 * Created by weikailiang on 2019/9/18.
 */

public class LoginActivity extends CBaseMvpActivity<LoginPresenter> implements LoginView{
    protected ActivityLoginBinding mBinding;
    Rotate3D anim;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        mBinding = (ActivityLoginBinding) mRootBinding;
        mPresenter = new LoginPresenter(this,this);
        mPresenter.start();
    }

    @Override
    protected void start() {
        BaseApp.removeActivity(this);
        mBinding.agreeMent.setChecked(true);
        anim = new Rotate3D.Builder(this)
                .bindParentView(mBinding.parentLl)
                .bindPositiveView(mBinding.accountPhoneLl)
                .bindNegativeView(mBinding.accountLoginLl)
                .create();
        mBinding.noPassLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.transform();
                if (anim.isOpen()) {
                    mBinding.noPassLogin.setTextColor(ContextCompat.getColor(mContext,R.color.color_4486FF));
                    mBinding.noPassLogin.setTextSize(18);
                    mBinding.tvReg.setTextColor(ContextCompat.getColor(mContext,R.color.color_tab_text_normal));
                    mBinding.tvReg.setTextSize(16);
                } else {
                    mBinding.noPassLogin.setTextColor(ContextCompat.getColor(mContext,R.color.color_tab_text_normal));
                    mBinding.noPassLogin.setTextSize(16);
                    mBinding.tvReg.setTextColor(ContextCompat.getColor(mContext,R.color.color_4486FF));
                    mBinding.tvReg.setTextSize(18);
                }
            }
        });
        mBinding.tvReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.transform();
                if (anim.isOpen()) {
                    mBinding.noPassLogin.setTextColor(ContextCompat.getColor(mContext,R.color.color_4486FF));
                    mBinding.noPassLogin.setTextSize(18);
                    mBinding.tvReg.setTextColor(ContextCompat.getColor(mContext,R.color.color_tab_text_normal));
                    mBinding.tvReg.setTextSize(16);
                } else {
                    mBinding.noPassLogin.setTextColor(ContextCompat.getColor(mContext,R.color.color_tab_text_normal));
                    mBinding.noPassLogin.setTextSize(16);
                    mBinding.tvReg.setTextColor(ContextCompat.getColor(mContext,R.color.color_4486FF));
                    mBinding.tvReg.setTextSize(18);
                }
            }
        });
        mBinding.sendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBinding.sendCode.getText().equals("发送验证码")){
                    if (TextUtils.isEmpty(mBinding.loginInputPhone.getText().toString())){
                        showMes("请输入手机号码");
                        return;
                    }else {
                        mPresenter.SendCode(mBinding.loginInputPhone.getText().toString());
                    }
                }else {
                    showMes("验证码已发送");
                    return;
                }
            }
        });
        mBinding.companyLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBinding.agreeMent.isChecked()){
                    showMes("请同意注册协议");
                    return;
                }
            }
        });
        mBinding.phoneLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBinding.agreeMent.isChecked()){
                    showMes("请同意注册协议");
                    return;
                }
                if (TextUtils.isEmpty(mBinding.loginInputPhone.getText().toString())){
                    showMes("请输入手机号码");
                    return;
                }
//                if (!mBinding.loginInputPhone.getText().toString().equals(mPresenter.sendPhone)){
//                    showMes("输入框内手机号和发送验证码手机号不符");
//                    return;
//                }
                if (TextUtils.isEmpty(mBinding.loginInputCode.getText().toString())){
                    showMes("请输入验证码");
                    return;
                }
                if (mBinding.loginInputCode.getText().toString().length()<4){
                    showMes("请输入正确验证码");
                    return;
                }
                mPresenter.phoneLogin(mBinding.loginInputPhone.getText().toString(),mBinding.loginInputCode.getText().toString());
            }
        });
        mBinding.agreeMent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked){
                    mBinding.phoneLogin.setEnabled(false);
                    mBinding.companyLogin.setEnabled(false);
                }else {
                    mBinding.phoneLogin.setEnabled(true);
                    mBinding.companyLogin.setEnabled(true);
                }
            }
        });

        mBinding.forgetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/changepw/pw").navigation();
            }
        });
        mBinding.register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMes("暂未开放");
            }
        });

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }

    @Override
    public TextView returnSendCodeView() {
        return mBinding.sendCode;
    }
}
